//引入样式
require('../css/commonCss/normalize.css')
require('../css/commonCss/reset.css')
require('../css/commonCss/property.css')
require('../css/commonCss/basic.less')

require('../fonts/iconfont.css')
require('animate.css')
require('../css/login.less')

//引入axios
const axios = require('axios')
//weui—插件库
require('weui')
const weui = require('weui.js')

/**
 * 0.获取元素
 */
const account = document.querySelector('#account')//账号
const password = document.querySelector('#password')//密码
const submitBtn = document.querySelector('#submitBtn')//登录
const pLogin = document.querySelector('.p-login')//注册账号
//  console.log(account,password,submitBtn);
/**
 *  0需求:点击注册账号跳转到注册页面
 */
pLogin.addEventListener('click',function(){
    location.href = './register.html'
})
/**
 * 1.需求: 点击注册按钮,触发注册验证
 */
submitBtn.addEventListener('click', function () {
    const accountVal = account.value; //账号
    const passwordVal = password.value; //账号

    if (!/^1[3-9][0-9]{9}$/.test(accountVal)) {
        errMsg.textContent = '账号错误'
        return;
    } else if (!/^[a-zA-Z]\w{5,11}$/.test(passwordVal)) {
        errMsg.textContent = '密码错误'
        return
    } else {
        // 清空错误信息
        errMsg.textContent = ''
        account.value = password.value = ''
        // 发送请求——注册账号
        axios.post('http://139.9.177.51:8099/users/login', {
            account: accountVal,
            password: passwordVal
        }).then(function(res){
            // console.log(res);
            if(res.data.status === 0){
                // 将用户id存入本地存储
                localStorage.setItem('userId',res.data.data.user.userId)
                // 成功
                // 跳转页面
                weui.toast('登录成功',{
                    duration:1000,
                    // className:'custom-className',//自定义类名
                    callback:function(){//回调函数
                        location.href = './home.html'
                    }
                })
            }else{
                // 失败
                weui.alert(res.data.msg)
                // alert(res.data.msg = '用户名或密码错误！')
            }
        })
    }
})